<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// ------------------------------------------------------------------------

/**
 * Application
 *
 */
class Deploy_online extends K9_Deploy_Controller
{
	/**
     * Constructor
     *
     * @access  public
     * @return  void
     */
	public function __construct()
	{
		parent::__construct();	
		$this->_check_permit();
		$this->load->model('deploy_task_model');
		$this->load->model('deploy/account_model');
		$this->load->model('deploy/commit_log_model');
		$this->load->model('deploy/app_info_model');
		$this->load->helper('str');
	}
	
	// ------------------------------------------------------------------------
	
	/**
     * Default entrance
     *
     * @access  public
     * @param   int
     * @return  void
     */
	public function view($app = 0, $env = 0, $class = 0)
	{
		$data['language'] = $this->language;
		
		if ($this->session->userdata('uid'))
		{	
			$data['current_app'] = $app;
			$data['current_env'] = $env;
			$data['current_class'] = $class;
			$deploy_task_count = $this->deploy_task_model->count();
			$data['classs'] = $this->_get_class();
			$data['apps'] = $this->_get_apps($data['current_class']);
			$data['envs'] = $this->_get_envs();
			$data['list'] = $this->deploy_task_model->get_online_tasks();
			$this->_template ( $this->language . '/deploy_online', $data );
		}
		else
		{
			$this->load->view($this->language.'/login',$data);
		}
	}

	public function add_task(){
                $app = $this->input->get_post('app',TRUE);
                $env = $this->input->get_post('env',TRUE);
                $back_commit = $this->input->get_post('last_commit',TRUE)?$this->input->get_post('last_commit',TRUE):'null';
                $ip = $this->input->ip_address();
                $user = $this->account_model->get_username($this->session->userdata('uid'));
                $appid = $this->app_info_model->get_appid($app);
                $data = array("appid" => $appid,"env" => $env, "ip" => $ip, "date" => time(),"deploy_user" => $user,"back_commit" => $back_commit);

                $this->deploy_task_model->add_task($data);
                $this->_message("添加成功");
        }

        public function del($task = 0){
                if($this->deploy_task_model->del_task($task)){
                        $this->_message("删除成功");
                }
        }

	public function update_content($app){
		$this->_template($this->language."/update_content",array('app'=>$app));
	}	
		

        private function _get_class(){
                $class = array('平台','平台支撑','其他','all');
                return $class;
        }

        private function _get_apps($class){
		$apps  = $this->app_info_model->get_apps($class);
                return $apps;
        }

        private function _get_envs(){
		$user = $this->account_model->get_username($this->session->userdata('uid'));
                $envs = array('online','ope','sandbox');
               	return $envs;
        }

	/**
         * 信息提示
         *
         * @access  public
         * @param $msg
         * @param string $goto
         * @param bool $auto
         * @param string $fix
         * @param int $pause
         * @return  void
         */
        public function _message($msg, $goto = '', $auto = TRUE, $fix = '', $pause = 6000)
        {
                if($goto == '')
                {
                        $goto = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : site_url();
                }
                else
                {
                        $goto = strpos($goto, 'http') !== false ? $goto : backend_url($goto);
                }
                $goto .= $fix;
                $this->load->view($this->language.'/sys_message', array('msg' => $msg, 'goto' => $goto, 'auto' => $auto, 'pause' => $pause));
                echo $this->output->get_output();
                exit();
        }

	/*
         *
         *
         * 回滚按钮
         *
         *
         *
         */
        public function rollback($app,$env)
        {
                $app = $app;
                $env = $env;
                $data['list'] = array("app" => $app,"env" => $env);
                $this->_template ( $this->language . '/back_commit', $data );
        }

        /*
         *
         *
         * 回滚按钮
         *
         *
         *
         */
        public function start_rollback()
        {
                $app = $this->input->get_post('app',TRUE);
                $env = $this->input->get_post('env',TRUE);
                $back_commit = $this->input->get_post('last_commit',TRUE);
                if(empty($back_commit)){
                        $this->_message("回滚版本不能为空.");
                        exit();
                }
                $ip = $this->input->ip_address();
                $user = $this->account_model->get_username($this->session->userdata('uid'));
                $appid = $this->app_info_model->get_appid($app);
                $data = array("appid" => $appid,"env" => $env, "ip" => $ip, "date" => time(),"deploy_user" => $user,"back_commit" => $back_commit);
                if($env=="online"){
                        $this->_message("禁止非法操作，你没有管理员权限!");
                        exit();
                }
                $this->deploy_task_model->add_task($data);
                $this->_message("添加成功");
        }

		
	public function deploying($app)
        {
                $data['app'] = $app;
                $this->_template($this->language.'/deploying',$data);
        }


}
